package com.mongoplus.solon.controller;

import com.anwen.mongo.mapper.MongoPlusMapMapper;
import com.mongoplus.solon.entity.User;
import com.mongoplus.solon.service.UserService;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

import java.util.HashMap;

/**
 * 用户控制类
 *
 * @author JiaChaoYang
 **/
@Controller
public class UserController {

    @Inject
    private UserService userService;

    @Inject
    private MongoPlusMapMapper mongoPlusMapMapper;

    @Get
    @Mapping("/findAllUser")
    public Object findAllUser(){
//        List<Map<String, Object>> mapList = mongoPlusMapMapper.list("user");
        User user = userService.lambdaQuery().orderByDesc(User::getCreateTime).limitOne();
//        Map<String, Object> user = mongoPlusMapMapper.getById("user", null);
        return user;
    }

    @Get
    @Mapping("/addUser")
    public Object addUser(){
        Boolean save = mongoPlusMapMapper.save("user", new HashMap<String,Object>(){{
            put("userName","我要测试我要测试");
            put("userStatus",1);
            put("age",21);
            put("role",new HashMap<String,Object>(){{
                put("roleName","普通用户");
                put("roleIntroduce","没啥权限");
            }});
        }});
        System.out.println(save?"添加成功":"添加失败");
        return save;
    }

}
